package com.steptime.framework.spring.aop.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;



public class CglibProxy implements MethodInterceptor {
	
	private UserDaoImpl userDao;
	
	public CglibProxy(UserDaoImpl userDao) {
		this.userDao = userDao;
	}
	
	public Object createProxy() {
		//1、创建代理生成类
		Enhancer enhancer = new Enhancer();
		//2、设置父类
		enhancer.setSuperclass(userDao.getClass());
		//3、设置回调(增强)
		enhancer.setCallback(this);
		//4、创建代理
		return enhancer.create();
	}

	@Override
	public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
		if("getUserInfo".equals(method.getName())) {
			System.out.println("权限验证....");
		}
		//调用父类方法
		return methodProxy.invokeSuper(proxy, args);
	}

}
